﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;
using System.Web.Security; 

namespace websitev2
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );


            // This is the routing for the generic stats
            routes.MapRoute(
                "Generic",
                "{controller}/User/{id}/{id2}/{action}",
                new { Controller = "DetailStats", action = "Index" }, new[] { "websitev2.Controllers" }
                ); 

            routes.MapRoute(
                "MoreDetail", // Route name
                "{controller}/{action}/{id}/{id2}", new { controller = "Home", action = "", id = "", id2="" }, new []{"websitev2.Controllers"}// URL with parameters
                
            );


            // This is the routing for the top apps
            routes.MapRoute(
                "Top",
                "Stats/User/{id}/{controller}/{action}/{id2}" , new [] {"websitev2.Controllers"}); 



        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

      

        protected void Application_Error()
        {

            // here we need to check to make sure this is not on the local host as we don't want to catch those
            if (Request.ServerVariables["SERVER_NAME"].ToString() != "localhost")
            {
                // defualt to using my error handling. 
                bool _errors = true; 
                try {
                    _errors = bool.Parse(ConfigurationManager.AppSettings["my.errors"].ToString()); 
                }
                catch { }
                if (_errors)
                {
                    var exception = Server.GetLastError();
                    var httpException = exception as HttpException;
                    Response.Clear();
                    Server.ClearError();
                    var routeData = new RouteData();
                    routeData.Values["controller"] = "Errors";
                    routeData.Values["action"] = "General";
                    routeData.Values["exception"] = exception;
                    Response.StatusCode = 500;
                    if (httpException != null)
                    {
                        Response.StatusCode = httpException.GetHttpCode();
                        switch (Response.StatusCode)
                        {
                            case 403:
                                routeData.Values["action"] = "Http403";

                                break;
                            case 404:
                                routeData.Values["action"] = "Http404";

                                break;
                        }
                    }

                    // Avoid IIS7 getting in the middle
                    Response.TrySkipIisCustomErrors = true;
                    IController errorsController = new Controllers.ErrorsController();
                    HttpContextWrapper wrapper = new HttpContextWrapper(Context);
                    var rc = new RequestContext(wrapper, routeData);
                    errorsController.Execute(rc);
                } // end if errors
            } // end if local server
        }
    }
}